finp$="video.flv"

'- video to animated-gif zxspectrum converter version 0809111825
'- sdlbasic version 0708072157
'- from firstbasic version: 0201310133
'- needs imagemagick, gifsicle and ffmpeg installed - Linux (or Unix) only?
'- gthumb used for previewing
'- important: depending on the video you have, the .gif result may get quite large (150frames~=1mb)
'- temporary files (.png) also take some significant room as well
'- the conversion takes a time around 25 frames per minute on a core2duo 1.8ghz with linux-i386

'- numeric values into strings: empty strings means no command line entering - ffmpeg time set in seconds
fps$="15":timestart$="15":timedura$="7":gifdelay$="10"

xed=256:yed=192:setdisplay(xed,yed,32,1):paper(8^8-1):ink(0):pen(0):cls:ftempref$="_tmpr"

'- shell:  ffmpeg -i teste01.flv -r 4 -s 256x192 -ss 48 -t 5 _tmpr%d.png
shelout$="ffmpeg -i "+finp$
if fps$<>"" then: shelout$=shelout$+" -r "+fps$: end if
shelout$=shelout$+" -s 256x192"
if timestart$<>"" then: shelout$=shelout$+" -ss "+timestart$: end if
if timedura$<>"" then: shelout$=shelout$+" -t "+timedura$: end if
shelout$=shelout$+" "+ftempref$+"%d.png"
setcaption("ffmpeg: converting..."):shell(shelout$)
frst=0

'- counting frames
frm=1
while fileexists(ftempref$+str$(frm)+".png")<>0
  setcaption("counting frames: "+str$(frm)):frm=frm+1
wend

dim clust[8,8],r0[8,8],g0[8,8],b0[8,8],plte[16]
plte[ 0]=0x000000:plte[ 2]=0x0000B4:plte[ 1]=0xB40000:plte[ 3]=0xB400B4
plte[ 4]=0x00B400:plte[ 6]=0x00B4B4:plte[ 5]=0xB4B400:plte[ 7]=0xB4B4B4
plte[ 8]=0x000000:plte[10]=0x0000FF:plte[ 9]=0xFF0000:plte[11]=0xFF00FF
plte[12]=0x00FF00:plte[14]=0x00FFFF:plte[13]=0xFFFF00:plte[15]=0xFFFFFF
'- "00,12,03,15,08,04,11,07,02,14,01,13,10,06,09,05."
'- "00,06,08,14,02,12,04,10,08,14,00,06,04,10,02,12."
clstr$="00,06,08,14,02,12,04,10,08,14,00,06,04,10,02,12."
for y2=0 to 3:for x2=0 to 3:clust[x2,y2]=val(mid$(clstr$,((y2*4+x2)*3)+1,2)):next:next
hbedge=180 '- halfbright attr edge -zx32=218, zx32fs=153 , realthing=180?
rgrsp=30:ggrsp=30:bgrsp=30 '- for r, g and b saturation levels

for frq=1 to frm-1:setcaption("frame "+str$(frq)+"/"+str$(frm-1))
framenm$=ftempref$+str$(frq)+".png":loadimage(framenm$,1):pasteicon(0,0,1)

for y1=0 to int(yed/8):for x1=0 to int(xed/8)
    for y2=0 to 7:for x2=0 to 7
        y=y1*8+y2:x=x1*8+x2
        zzz=point(x,y)
        r0[x2,y2]=bitwiseand(zzz,255):zzz=int(zzz/256)
        g0[x2,y2]=bitwiseand(zzz,255):zzz=int(zzz/256)
        b0[x2,y2]=bitwiseand(zzz,255)
        next:next
    bi=0:ri=0:gi=0
    for y2=0 to 7:for x2=0 to 7
        x=(x1*8)+x2:y=(y1*8)+y2
        bi=bi+b0[x2,y2]:gi=gi+g0[x2,y2]:ri=ri+r0[x2,y2]
        next:next
    b=bi/64:g=gi/64:r=ri/64:xrreg=0
    hbrite=0:if (r<hbedge) and(g<hbedge) and(b<hbedge) then:hbrite=1:end if
    hbampl=255-(hbrite*(255-hbedge))
    if b>(hbampl/2) then:b=(hbampl-b):xrreg=bitwiseor(xrreg,1):end if
    if r>(hbampl/2) then:r=(hbampl-r):xrreg=bitwiseor(xrreg,2):end if
    if g>(hbampl/2) then:g=(hbampl-g):xrreg=bitwiseor(xrreg,4):end if
    halbr=(r*rgrsp)/100:halbg=(g*ggrsp)/100:halbb=(b*bgrsp)/100:vlik=7
    if((r>halbb) and(g<=halbb)) or((b<=halbr) and(g<=halbr))then:vlik=3:end if
    if((g>halbb) and(r<=halbb)) or((b<=halbg) and(r<=halbg))then:vlik=5:end if
    if((g>halbr) and(b<=halbr)) or((r<=halbg) and(b<=halbg))then:vlik=6:end if
    if((r<=halbb) and(g<=halbb)) then: vlik=1:end if
    if((b<=halbr) and(g<=halbr)) then: vlik=2:end if
    if((b<=halbg) and(r<=halbg)) then: vlik=4:end if
    brattr=1-hbrite:ikattr=bitwisexor(vlik,xrreg):paattr=xrreg
    if ikattr<paattr then:tmpr=ikattr:ikattr=paattr:paattr=tmpr:end if
    ikval=ikattr+(bitwiseand(ikattr,6)/2)
    paval=paattr+(bitwiseand(paattr,6)/2)
    lumik=(ikval*255)/10:lumpa=(paval*255)/10
    if brattr<1 then:lumik=(lumik*hbedge)/255:lumpa=(lumpa*hbedge)/255:end if 
    dflum=lumik-lumpa
    for y2=0 to 7
      for x2=0 to 7
        y=y1*8+y2:x=x1*8+x2:b=b0[x2,y2]:g=g0[x2,y2]:r=r0[x2,y2]
        vlue=((b+(r*3)+(g*6))/10):patgf=(((clust[bitwiseand(x2,3),bitwiseand(y2,3)]+1)*255)/16)
        varnd=((patgf*dflum)/255)+lumpa:ik=ikattr+(8*brattr)
        if varnd>vlue then: ik=paattr+(8*brattr):end if
        ink(plte[ik]):dot(x,y)
        next
      yf=(bitwiseand(y,7)*8) or (bitwiseand(y,56)/8) or bitwiseand(y,192)
      next:next:next

grab (1,0,0,xed,yed):saveimage(ftempref$+str$(frq)+"_out.bmp",1)
shell("convert "+ftempref$+str$(frq)+"_out.bmp _tmpr.gif")

gifdelv=val(gifdelay$)

  if frst<>0 then:
    shell("gifsicle -l=forever -d="+str$(gifdelv)+" _tmpr1.gif _tmpr.gif > _tmpr2.gif")
  else
    shell("gifsicle -l=forever -d="+str$(gifdelv)+" _tmpr.gif > _tmpr2.gif"):frst=1
    end if

shell("rm "+ftempref$+str$(frq)+"_out.bmp "+ftempref$+str$(frq)+".png")
shell("rm _tmpr.gif _tmpr1.gif"):shell("mv _tmpr2.gif _tmpr1.gif")
next
shell("mv _tmpr1.gif "+finp$+".gif"):shell("gthumb "+finp$+".gif")



